<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    use HasFactory;

    protected $fillable = [
        'title','type','pay_type','manager','work_period','tender_date','brief','area','start_date','end_date','contract_number','contract','price','budget','start_actual_date','end_actual_date','address','status','construct_unit','build_unit','supervise_unit','design_unit','company_id','push_id','consortium_unit','operation_unit','notification_of_award','tax_rate','tax_type','structure_type','gradation_price','examine_price','account_price','pay_price','balance_price','no_pay_price','settlement','general_units','consortium_units'
    ];

    const POSTONLY = ['title','type','brief','manager','area','pay_type','work_period','tender_date','start_date','end_date','contract_number','contract','price','budget','start_actual_date','end_actual_date','address','status','construct_unit','build_unit','supervise_unit','design_unit','consortium_unit','operation_unit','notification_of_award','tax_rate','tax_type','structure_type','gradation_price','examine_price','account_price','pay_price','balance_price','no_pay_price','settlement','general_units','consortium_units'];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function users()
    {
        return $this->belongsToMany(User::class,'project_users')->withPivot('role_id','start_date','end_date');
    }

    public function teams()
    {
        return $this->belongsToMany(Team::class,'project_teams');
    }

    public function processess()
    {
        return $this->hasMany(ApplicationProcesses::class,'project_id','id')->with('user');
    }

    public function present()
    {
        return $this->hasOne(ProjectPresent::class,'project_id','id');
    }

    public function getUsers($id)
    {
        $user = $this->with('users')->where('id',$id)->first()->users;
        if(!$user){
            return false;
        }
        $u = [];
        foreach ($user as $key => $value) {
            $u[$value->name] = $value->id;
        }
        return $u;
    }

    public function remainDuration($date)
    {
        $data = strtotime($date) - time();
        $data = $data/3660/24;
        return round($data,0);
    }

    public function duration($start_date,$end_date)
    {
        $data = strtotime($end_date) - strtotime($start_date);
        $data = $data/3660/24;
        return round($data,0);
    }
}
